/**
 * 
 */
package com.vurenium.ras.util;
import java.sql.Connection;
import java.sql.Statement;

import javax.faces.model.ResultDataModel;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import javax.sql.DataSource;

/**
 * Helper methods for jdbc calls
 * @author svaish
 *
 */
public class DBHelper {
	
	public final static String DATASOURCE_NAME="jdbc/RasDB";

		public static Connection getConnection() throws Exception{
	        Context env = (Context) new InitialContext().lookup("java:comp/env");
	        DataSource dataSource = (DataSource) env.lookup(DATASOURCE_NAME);
			return dataSource.getConnection();
		}
		
		public static ResultDataModel select(String sql) throws DBException {
			try{
			Statement stmt = getConnection().createStatement();
			return new ResultDataModel(ResultSupport.toResult(stmt.executeQuery(sql)));
			}catch (Exception ex){
				throw new DBException(ex);
			}
		}

		public static int change(String sql) throws Exception {
			return 0;
		}


}
